<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/8/1
 * Time: 9:54
 */

namespace app\models;

use yii\base\Model;
use Yii;
use \common\helps\tools;//使用工具类

Class User extends Model
{
    //表名
    protected $table = 'd_user';


    /**
     * title 删除 用户
     * @param $id
     * @throws \yii\db\Exception
     */
    public function delUser($id){
        $del = Yii::$app->db->createCommand()->delete(tools::databaseName(). '.' .$this->table, "id = '$id'")->execute();
        return $del;
    }


    /**
     * title 单条查询用户
     * @param $id
     * @return mixed
     */
    public function findUser($id){
        $query = new \yii\db\Query();
        $find = $query->select('*')->from(tools::databaseName(). '.' .$this->table)->where(['id'=>$id])->one();
        return $find;

    }


    /**
     * title 添加用户
     * @param $user_id
     * @param $phone
     * @param $company
     * @param $name
     * @param $job
     * @param $password
     * @return int
     * @throws \yii\db\Exception
     */
    public function addUser($id,$phone,$company,$name,$job,$create_time)
    {
        $password = md5('123456');

        $field = ['id','phone','company','name','job','password','create_time'];
        $batchData = [[$id,$phone,$company,$name,$job,$password,$create_time]];
        $user = Yii::$app->db->createCommand()->batchInsert(tools::databaseName().'.'.$this->table, $field, $batchData)->execute();
        return $user;
    }


    /**
     * title 根据电话查询用户
     * @param $phone
     * @return array|bool
     */
    public function phoneUser($phone){
        $query = new \yii\db\Query();
        $find = $query->select('*')->from(tools::databaseName(). '.' .$this->table)->where(['phone'=>$phone])->one();
        return $find;

    }


}